package com.arrogames.rgblabyrinth.controller {
	import com.arrogames.rgblabyrinth.menu.events.DeleteGameEvent;
	import com.arrogames.rgblabyrinth.menu.events.OptionsMenuEvent;
	import com.arrogames.rgblabyrinth.menu.events.StartGameEvent;
	import com.arrogames.rgblabyrinth.model.storage.GameMetaData;
	
	import starling.display.DisplayObject;

	public class RGBController {

		private var model:IRGBModel;
		private var view:DisplayObject;
		
		public function RGBController(model:IRGBModel, view:DisplayObject) {
			this.model = model;
			this.view = view;
			
			view.addEventListener(StartGameEvent.NEWGAME, onNewGame);
			view.addEventListener(StartGameEvent.CONTINUEGAME, onContinueGame);
			view.addEventListener(DeleteGameEvent.DELETE, onDeleteGame);
			view.addEventListener(OptionsMenuEvent.EXITGAME, onExitGame);
			view.addEventListener(OptionsMenuEvent.TOGGLE_MUSIC, onToggleMusic);
			view.addEventListener(OptionsMenuEvent.TOGGLE_SFX, onToggleSfx);
			view.addEventListener(OptionsMenuEvent.TOGGLE_PAUSE, onTogglePauseGame);
		}
		
		private function onNewGame(event:StartGameEvent):void {
			var color:uint = event.data as uint;
			model.startNewGame(color);
		}
		
		private function onContinueGame(event:StartGameEvent):void {
			var game:GameMetaData = event.data as GameMetaData;
			model.continueGame(game);
		}
		
		private function onDeleteGame(event:DeleteGameEvent):void {
			var game:GameMetaData = event.data as GameMetaData;
			model.removeGame(game);
		}
		
		private function onExitGame():void {
			model.stopCurrentGame();
		}
		
		private function onTogglePauseGame():void {
			model.isPaused = !model.isPaused;
		}
		
		private function onToggleSfx():void {
			model.isSfxEnabled = !model.isSfxEnabled;
		}
		
		private function onToggleMusic():void {
			model.isMusicEnabled = !model.isMusicEnabled;
		}
	}
}